<template>
  <div class="app-container">
    <el-form
      ref="dataForm"
      :model="config"
      :rules="rules"
      label-position="right"
      label-width="100px"
    >
      <el-form-item label="邮箱" prop="emil">
        <el-input
          v-model.trim="config.emil"
          placeholder="请输入接收提醒的邮箱"
          class="w"
        />
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >接收提醒的邮箱</span
        >
      </el-form-item>
      <el-form-item label="默认百分比" prop="def">
        <el-input
          v-model.number="config.def"
          placeholder="默认百分比"
          class="w"
        ><template slot="append">%</template></el-input>
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >对比降低百分比提醒</span
        >
      </el-form-item>
      <div class="box">
        <el-button type="primary" @click="addChannel()" size="small"
          >添加渠道
        </el-button>
        <span style="font-size: 12px; color: #999; padding-top: 10px"
          >渠道单独设置</span
        >
      </div>

      <template>
        <div v-for="(v, i) in config.channels" :key="i" class="box">
          <el-form-item
            label="渠道"
            :prop="'channels.' + i + '.channel'"
            :rules="[{ required: true, message: '必填', trigger: 'blur' }]"
          >
            <el-input
              v-model.trim="config.channels[i].channel"
              placeholder="请输入渠道名称"
              class="w"
            />
          </el-form-item>

          <el-form-item
            label="百分比"
            :prop="'channels.' + i + '.percent'"
            :rules="[{ required: true, message: '必填', trigger: 'blur' }]"
          >
            <el-input
              v-model.number="config.channels[i].percent"
              placeholder="渠道百分比"
              class="w"
            >
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <div style="padding-left: 10px;padding-top:5px">
              <el-button type="primary" @click="delChannel(i)" size="small"
            >删除渠道
          </el-button>
          </div>
          
        </div>
      </template>

      <div style="text-align: left">
        <el-button
          type="primary"
          @click="updateData()"
          :loading="updateLoading"
        >
          确定修改
        </el-button>
      </div>
    </el-form>
    <div class ="table">

      <el-form :inline="true" :model="query" class="demo-form-inline">
        <el-form-item label="渠道">
          <el-input v-model="query.channel" placeholder="渠道"></el-input>
        </el-form-item>
      
        <el-form-item>
          <el-button type="primary" @click="get_list">查询</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resert">重制</el-button>
        </el-form-item>
      </el-form>


      <el-table
        :data="list"
        height="500"
        border
        v-loading="listLoading"
        style="width: 100%">
          <el-table-column
            prop="time_"
            label="日期"
            width="180">
          </el-table-column>
          <el-table-column
            prop="username"
            label="渠道"
            width="180">
          </el-table-column>
          <el-table-column prop="oppo" label="oppo对比下降">
            <template slot-scope="{row}">
              <span>{{ row.oppo ? '下降'+ row.oppo + '%' : '--' }}</span>
            </template> 
        </el-table-column>
          <el-table-column prop="vivo" label="vivo对比下降">
            <template slot-scope="{row}">
              <span>{{ row.vivo ? '下降'+ row.vivo + '%' : '--'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="xiaomi" label="xiaomi对比下降">
            <template slot-scope="{row}">
              <span>{{ row.xiaomi ? '下降'+ row.xiaomi + '%' : '--' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="huawei" label="huawei对比下降">
            <template slot-scope="{row}">
              <span>{{ row.huawei ? '下降'+ row.huawei + '%' : '--' }}</span>
            </template> 
          </el-table-column>
          <el-table-column prop="remark" label="邮件">
            <template slot-scope="{row}">
              <span>{{ row.remark == '1' ? '已发送' : row.remark }}</span>
            </template> 
          </el-table-column>
      </el-table>
       <pagination v-show="total > 0" :total="total" class="pageShow" :page.sync="query.page" :limit.sync="query.page_size" @pagination="get_list" />
    </div>
  </div>
</template>
<script>
import {
  get_uv_monitoring_config,
  set_uv_monitoring_config,
  get_uv_monitoring_history,
  del_uv_monitoring_history
} from "@/api/cnzz/dashboard";
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data() {
    return {
      config: {},
      updateLoading: false,
      rules: {
        emil: [{ required: true, message: "必填", trigger: "blur" }],
        def: [{ required: true, message: "必填", trigger: "blur" }],
      },
      listLoading: false,
      list: [],
      total: 0,
      query: {
        'page': 1,
        'page_size': 20,
        'channel':"",
        'is_fuzhou': '1'
      },
    };
  },
  mounted() {
    this.get_config();
     this.get_list();
  },
  methods: {
    get_config() {
      get_uv_monitoring_config({is_fuzhou: '1'}).then((res) => {
        if (res.STATUS == "200") {
          this.config = res.DATA;
        } else {
          this.$message.error(res.DEC);
        }
      });
    },
    get_list(){
      this.listLoading = true
      get_uv_monitoring_history(this.query).then(res => {
        if (res.STATUS == '200'){
          this.list = res.DATA.list
          this.total = res.DATA.total;
        } else {
          this.$message.error(res.DEC)
        }
        this.listLoading = false
      })
    },
    resert(){
      this.query = {
        'page': 1,
        'page_size': 20,
        'channel':""
      }
      this.get_list()
    },
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.updateLoading = true;
          set_uv_monitoring_config({
            is_fuzhou: '1',
            data: JSON.stringify(this.config),
          }).then((res) => {
            if (res.STATUS == "200") {
              this.$message.success("修改成功！");
            } else {
              this.$message.error(res.DEC);
            }
            this.updateLoading = false;
          });
        }
      });
    },
    addChannel() {
      this.config.channels.push({ channel: "", percent: 0});
    },
    delChannel(i) {
      this.config.channels.splice(i, 1);
    },
  },
};
</script>
<style>
.w {
  width: 200px;
}
.box {
  padding-bottom: 10px;
  display: flex;
  justify-self: start;
}
</style>